{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# {class}`~tvm.contrib.msc.framework.tensorflow.runtime.TensorflowRunner`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/media/pc/data/lxw/ai/tvm-book/doc/tutorials/msc\n"
     ]
    }
   ],
   "source": [
    "%cd ..\n",
    "from pathlib import Path\n",
    "\n",
    "temp_dir = Path(\".temp\")\n",
    "temp_dir.mkdir(exist_ok=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "构建前端模型："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024-12-31 17:42:47.206712: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n",
      "2024-12-31 17:42:47.504784: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n",
      "2024-12-31 17:42:47.619618: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n",
      "2024-12-31 17:42:48.380815: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n",
      "To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n",
      "2024-12-31 17:43:08.227733: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n"
     ]
    }
   ],
   "source": [
    "from tvm.contrib.msc.framework.tensorflow import tf_v1\n",
    "import tvm\n",
    "\n",
    "\n",
    "def _get_tf_graph():\n",
    "    \"\"\"Get tensorflow graphdef\"\"\"\n",
    "\n",
    "    # pylint: disable=import-outside-toplevel\n",
    "    try:\n",
    "        import tvm.relay.testing.tf as tf_testing\n",
    "\n",
    "        tf_graph = tf_v1.Graph()\n",
    "        with tf_graph.as_default():\n",
    "            graph_def = tf_testing.get_workload(\n",
    "                \"https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.4_224.tgz\",\n",
    "                \"mobilenet_v2_1.4_224_frozen.pb\",\n",
    "            )\n",
    "            # Call the utility to import the graph definition into default graph.\n",
    "            graph_def = tf_testing.ProcessGraphDefParam(graph_def)\n",
    "        return tf_graph, graph_def\n",
    "    except:  # pylint: disable=bare-except\n",
    "        print(\"please install tensorflow package\")\n",
    "        return None, None\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "tags": [
     "hide-output"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /media/pc/data/lxw/ai/tvm/python/tvm/relay/testing/tf.py:282: FastGFile.__init__ (from tensorflow.python.platform.gfile) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use tf.gfile.GFile.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024-12-31 17:43:35.660493: I tensorflow/core/common_runtime/gpu/gpu_device.cc:2021] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 21128 MB memory:  -> device: 0, name: NVIDIA GeForce RTX 3090, pci bus id: 0000:03:00.0, compute capability: 8.6\n",
      "2024-12-31 17:43:35.661150: I tensorflow/core/common_runtime/gpu/gpu_device.cc:2021] Created device /job:localhost/replica:0/task:0/device:GPU:1 with 9796 MB memory:  -> device: 1, name: NVIDIA GeForce RTX 2080 Ti, pci bus id: 0000:81:00.0, compute capability: 7.5\n",
      "2024-12-31 17:43:35.750451: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:388] MLIR V1 optimization pass is not enabled\n",
      "2024-12-31 17:43:37.934376: I external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:531] Loaded cuDNN version 8907\n",
      "WARNING: All log messages before absl::InitializeLog() is called are written to STDERR\n",
      "W0000 00:00:1735638218.239880   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.328986   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.329503   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.330011   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.330546   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.348735   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.351643   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.354133   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.356545   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.357043   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.443659   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.446632   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.449614   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.450069   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.450510   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.450940   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.451378   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.469990   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.473206   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.476641   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.479949   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.483028   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.508529   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.509151   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.511762   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.512336   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.515244   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.517987   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.518533   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.519037   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.522066   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n",
      "W0000 00:00:1735638218.525022   29375 gpu_timer.cc:114] Skipping the delay kernel, measurement accuracy will be reduced\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "`batch_normalization` is not available with Keras 3.",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[3], line 22\u001b[0m\n\u001b[1;32m     20\u001b[0m mod, _ \u001b[38;5;241m=\u001b[39m from_tensorflow(graph_def, shape_dict, [out_name], as_msc\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[1;32m     21\u001b[0m runner \u001b[38;5;241m=\u001b[39m TensorflowRunner(mod)\n\u001b[0;32m---> 22\u001b[0m runner\u001b[38;5;241m.\u001b[39mbuild()\n\u001b[1;32m     23\u001b[0m outputs \u001b[38;5;241m=\u001b[39m runner\u001b[38;5;241m.\u001b[39mrun([data], ret_type\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mlist\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m     24\u001b[0m workspace\u001b[38;5;241m.\u001b[39mdestory()\n",
      "File \u001b[0;32m/media/pc/data/lxw/ai/tvm/python/tvm/contrib/msc/core/runtime/runner.py:249\u001b[0m, in \u001b[0;36mBaseRunner.build\u001b[0;34m(self, cache_dir, force_build, disable_tools)\u001b[0m\n\u001b[1;32m    246\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m    247\u001b[0m         \u001b[38;5;66;03m# Generate normal model\u001b[39;00m\n\u001b[1;32m    248\u001b[0m         \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_graphs, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_weights \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mreset_tools(tools\u001b[38;5;241m=\u001b[39mtools, cache_dir\u001b[38;5;241m=\u001b[39mcache_dir)\n\u001b[0;32m--> 249\u001b[0m         \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_model \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mgenerate_model()\n\u001b[1;32m    250\u001b[0m     build_msg \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mGenerate \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    252\u001b[0m \u001b[38;5;66;03m# Add tool message\u001b[39;00m\n",
      "File \u001b[0;32m/media/pc/data/lxw/ai/tvm/python/tvm/contrib/msc/core/runtime/runner.py:441\u001b[0m, in \u001b[0;36mBaseRunner.generate_model\u001b[0;34m(self, apply_hooks)\u001b[0m\n\u001b[1;32m    439\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m hook \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_generate_config\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpre_hooks\u001b[39m\u001b[38;5;124m\"\u001b[39m, []):\n\u001b[1;32m    440\u001b[0m         graphs, weights \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_apply_hook(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mbefore generate\u001b[39m\u001b[38;5;124m\"\u001b[39m, hook, graphs, weights)\n\u001b[0;32m--> 441\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_generate_model(graphs, weights)\n\u001b[1;32m    442\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m apply_hooks:\n\u001b[1;32m    443\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m hook \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_generate_config\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpost_hooks\u001b[39m\u001b[38;5;124m\"\u001b[39m, []):\n",
      "File \u001b[0;32m/media/pc/data/lxw/ai/tvm/python/tvm/contrib/msc/framework/tensorflow/runtime/runner.py:112\u001b[0m, in \u001b[0;36mTensorflowRunner._generate_model\u001b[0;34m(self, graphs, weights)\u001b[0m\n\u001b[1;32m    110\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_tf_graph \u001b[38;5;241m=\u001b[39m tf_v1\u001b[38;5;241m.\u001b[39mGraph()\n\u001b[1;32m    111\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_tf_graph\u001b[38;5;241m.\u001b[39mas_default():\n\u001b[0;32m--> 112\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_tf_outputs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28msuper\u001b[39m()\u001b[38;5;241m.\u001b[39m_generate_model(graphs, weights)\n\u001b[1;32m    113\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_tf_graph\n",
      "File \u001b[0;32m/media/pc/data/lxw/ai/tvm/python/tvm/contrib/msc/core/runtime/runner.py:1229\u001b[0m, in \u001b[0;36mModelRunner._generate_model\u001b[0;34m(self, graphs, weights)\u001b[0m\n\u001b[1;32m   1213\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_generate_model\u001b[39m(\u001b[38;5;28mself\u001b[39m, graphs: List[MSCGraph], weights: Dict[\u001b[38;5;28mstr\u001b[39m, tvm\u001b[38;5;241m.\u001b[39mnd\u001b[38;5;241m.\u001b[39marray]) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Any:\n\u001b[1;32m   1214\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124;03m\"\"\"Codegen the model according to framework\u001b[39;00m\n\u001b[1;32m   1215\u001b[0m \n\u001b[1;32m   1216\u001b[0m \u001b[38;5;124;03m    Parameters\u001b[39;00m\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m   1226\u001b[0m \u001b[38;5;124;03m        The runnable model\u001b[39;00m\n\u001b[1;32m   1227\u001b[0m \u001b[38;5;124;03m    \"\"\"\u001b[39;00m\n\u001b[0;32m-> 1229\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcodegen_func(\n\u001b[1;32m   1230\u001b[0m         graphs[\u001b[38;5;241m0\u001b[39m],\n\u001b[1;32m   1231\u001b[0m         weights,\n\u001b[1;32m   1232\u001b[0m         codegen_config\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_generate_config\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcodegen\u001b[39m\u001b[38;5;124m\"\u001b[39m),\n\u001b[1;32m   1233\u001b[0m         print_config\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_generate_config\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mprint\u001b[39m\u001b[38;5;124m\"\u001b[39m),\n\u001b[1;32m   1234\u001b[0m         build_folder\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_generate_config[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mbuild_folder\u001b[39m\u001b[38;5;124m\"\u001b[39m],\n\u001b[1;32m   1235\u001b[0m         plugin\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_plugin,\n\u001b[1;32m   1236\u001b[0m     )\n",
      "File \u001b[0;32m/media/pc/data/lxw/ai/tvm/python/tvm/contrib/msc/framework/tensorflow/codegen/codegen.py:72\u001b[0m, in \u001b[0;36mto_tensorflow\u001b[0;34m(graph, weights, codegen_config, print_config, build_folder, plugin)\u001b[0m\n\u001b[1;32m     70\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m plugin:\n\u001b[1;32m     71\u001b[0m     model_args \u001b[38;5;241m=\u001b[39m model_args \u001b[38;5;241m+\u001b[39m [plugin]\n\u001b[0;32m---> 72\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m codegen\u001b[38;5;241m.\u001b[39mload(model_args, pre_load\u001b[38;5;241m=\u001b[39m_save_weights)\n",
      "File \u001b[0;32m/media/pc/data/lxw/ai/tvm/python/tvm/contrib/msc/core/codegen/codegen.py:118\u001b[0m, in \u001b[0;36mCodeGen.load\u001b[0;34m(self, inputs, pre_load, post_load, build_model)\u001b[0m\n\u001b[1;32m    116\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_code_format \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpython\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m    117\u001b[0m     builder \u001b[38;5;241m=\u001b[39m msc_utils\u001b[38;5;241m.\u001b[39mload_callable(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_graph\u001b[38;5;241m.\u001b[39mname \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m.py:\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m+\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_graph\u001b[38;5;241m.\u001b[39mname)\n\u001b[0;32m--> 118\u001b[0m     obj \u001b[38;5;241m=\u001b[39m builder(\u001b[38;5;241m*\u001b[39minputs)\n\u001b[1;32m    119\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m    120\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mNotImplementedError\u001b[39;00m(\n\u001b[1;32m    121\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCode format \u001b[39m\u001b[38;5;132;01m{}\u001b[39;00m\u001b[38;5;124m is not supported\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m.\u001b[39mformat(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_code_format)\n\u001b[1;32m    122\u001b[0m     )\n",
      "File \u001b[0;32mmain.py:127\u001b[0m, in \u001b[0;36mmain\u001b[0;34m(res_0, weights)\u001b[0m\n",
      "File \u001b[0;32m/media/pc/data/lxw/envs/anaconda3x/envs/xxx/lib/python3.12/site-packages/tensorflow/python/util/lazy_loader.py:207\u001b[0m, in \u001b[0;36mKerasLazyLoader.__getattr__\u001b[0;34m(self, item)\u001b[0m\n\u001b[1;32m    200\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mAttributeError\u001b[39;00m(\n\u001b[1;32m    201\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m`tf.compat.v2.keras` is not available with Keras 3. Just use \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    202\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m`import keras` instead.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    203\u001b[0m     )\n\u001b[1;32m    204\u001b[0m   \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_tfll_submodule \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_tfll_submodule\u001b[38;5;241m.\u001b[39mstartswith(\n\u001b[1;32m    205\u001b[0m       \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m__internal__.legacy.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    206\u001b[0m   ):\n\u001b[0;32m--> 207\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mAttributeError\u001b[39;00m(\n\u001b[1;32m    208\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m`\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mitem\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m` is not available with Keras 3.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    209\u001b[0m     )\n\u001b[1;32m    210\u001b[0m module \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_load()\n\u001b[1;32m    211\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mgetattr\u001b[39m(module, item)\n",
      "\u001b[0;31mAttributeError\u001b[0m: `batch_normalization` is not available with Keras 3."
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from tvm.contrib.msc.framework.tensorflow.frontend import from_tensorflow\n",
    "from tvm.contrib.msc.framework.tensorflow.runtime import TensorflowRunner\n",
    "from tvm.contrib.msc.core import utils as msc_utils\n",
    "\n",
    "\n",
    "tf_graph, graph_def = _get_tf_graph()\n",
    "if tf_graph and graph_def:\n",
    "    path = f\"{temp_dir}/test_runner_tf\"\n",
    "    workspace = msc_utils.set_workspace(msc_utils.msc_dir(path, keep_history=False))\n",
    "    log_path = workspace.relpath(\"MSC_LOG\", keep_history=False)\n",
    "    msc_utils.set_global_logger(\"critical\", log_path)\n",
    "    data = np.random.uniform(size=(1, 224, 224, 3)).astype(\"float32\")\n",
    "    out_name = \"MobilenetV2/Predictions/Reshape_1:0\"\n",
    "    # get golden\n",
    "    with tf_v1.Session(graph=tf_graph) as sess:\n",
    "        golden = sess.run([out_name], {\"input:0\": data})\n",
    "    # get outputs\n",
    "    shape_dict = {\"input\": data.shape}\n",
    "    mod, _ = from_tensorflow(graph_def, shape_dict, [out_name], as_msc=False)\n",
    "    runner = TensorflowRunner(mod)\n",
    "    runner.build()\n",
    "    outputs = runner.run([data], ret_type=\"list\")\n",
    "    workspace.destory()\n",
    "    for gol_r, out_r in zip(golden, outputs):\n",
    "        tvm.testing.assert_allclose(gol_r, msc_utils.cast_array(out_r), atol=1e-3, rtol=1e-3)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "xxx",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
